package com.car.bussi.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.car.bussi.form.BusCarsForm;
import com.car.bussi.form.BusRentsForm;
import com.car.bussi.query.BusCarsQuery;
import com.car.bussi.service.IBussiBusCarsService;
import com.car.common.Result;
import com.car.common.util.WebUtil;
import com.car.common.validator.ValidatorUtil;

@RestController
@RequestMapping("/bussi/car")
public class BussiBusCarsController {

	@Autowired
	private IBussiBusCarsService bussiBusCarsService;

	/**
	 * @Title: list
	 * @author: TiTanLee   
	 * @date: 2019年12月12日 上午9:15:30 
	 * @Description: 分页查询所有车辆
	 * @param query
	 * @return
	 * @return: Object
	 */
	@RequestMapping("/list.do")
	public Object list(BusCarsQuery query) {
		return bussiBusCarsService.queryPage(query);
	}
	
	/**
	 * @Title: add
	 * @author: TiTanLee   
	 * @date: 2019年12月3日 下午9:45:39
	 * @Description: 添加车辆信息
	 * @param form
	 * @param carImg
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 * @return: Object
	 */
	@RequestMapping("/add.do")
	public Object add(BusCarsForm form, MultipartFile carImg) throws IllegalStateException, IOException {
		// 校验数据
		ValidatorUtil.validator(form);
		/*  保存图片
			 将图片的网络路径保存到数据库
			站到保存图片的文件夹 */
		String path = WebUtil.getSelServletContext().getRealPath("uploadImg");
		// 获取文件名
		String fileName = carImg.getOriginalFilename();
		fileName = UUID.randomUUID().toString() + fileName;
		File file = new File(path + File.separator + fileName);
		// 保存文件
		carImg.transferTo(file);
		form.setImg("uploadImg/" + fileName);
		Result rs = bussiBusCarsService.createNewCars(form);
		return rs;
	}
	
	/**
	 * @Title: update
	 * @author: TiTanLee   
	 * @date: 2019年12月3日 下午9:47:06 
	 * @Description: 修改车辆信息
	 * @param form
	 * @param carImg
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 * @return: Object
	 */
	@RequestMapping("/update.do")
	public Object update(BusCarsForm form,MultipartFile carImg) throws IllegalStateException, IOException {
		// 校验数据
		ValidatorUtil.validator(form);
		String path = WebUtil.getSelServletContext().getRealPath("uploadImg");
		String fileName = carImg.getOriginalFilename();
		fileName = UUID.randomUUID().toString() + fileName;
		File file = new File(path + File.separator + fileName);
		// 保存文件
		carImg.transferTo(file);
		form.setImg("uploadImg/" + fileName); 
		Result rs = bussiBusCarsService.updateCarInfo(form);
		return rs;
	}
	
	/**
	 * @Title: rent
	 * @author: TiTanLee   
	 * @date: 2019年12月3日 下午9:46:48 
	 * @Description: 出租记录
	 * @param form
	 * @return
	 * @return: Object
	 */
	@RequestMapping("/rent.do")
	public Object rent(BusRentsForm form) {
		// 1. 新增一条租车记录
		// 2. 修改当前车辆状态
		Result rs = bussiBusCarsService.rentCar(form);
		return rs;
	}

}
